package leetcode.year2021;

import java.util.ArrayList;
import java.util.List;

public class _04ByteDanceSencondIntView {
    public static void main(String[] args) {
        // Scanner input=new Scanner(System.in);
        // String str=input.next();
        System.out.println(mySplit("1,2,3", ","));
        System.out.println(mySplit("1,2,3",","));
        String[] strs = mySplit("1,2,3",",");
        for(int i = 0; i< strs.length; i++){
            System.out.println(strs[i]);
        }

    }

    public static String[] mySplit(String str, String spliter) {
        List<String> ansList = new ArrayList<>();
        int strLength = str.length();
        int spliterLength = spliter.length();
        if (strLength < spliterLength) {
            return new String[0];
        }
        int start = 0;
        int end = 0;
        for (int i = 0; i < strLength - spliterLength; i++) {
            if (isMatchSpliter(i, str, spliter, spliterLength)) {
                ansList.add(str.substring(start, end));
                start = i + spliterLength;
                i += spliterLength-1;
                end = start;
            } else {
                end++;
            }
        }
        ansList.add(str.substring(start));
        String[] ans = new String[ansList.size()];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = ansList.get(i);
        }
        return ans;
    }

    public static boolean isMatchSpliter(int index, String str, String spliter, int spliterLength) {
        for (int i = index; i < index + spliterLength; i++) {
            if (str.charAt(i) != spliter.charAt(i - index)) {
                return false;
            }
        }
        return true;
    }
}
